/**
 * 
 */
package com.jic.member.controller.manage;

import com.alibaba.fastjson.JSONArray;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.enums.RedisKey;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.MememberIntegralLog;
import com.jic.member.request.MememberIntegralLogQueryRequest;
import com.jic.member.request.MememberUserRequest;
import com.jic.member.response.MememberIntegralLogResponse;
import com.jic.member.service.MememberIntegralLogService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yinmenghui
 *
 */
@RestController
@RequestMapping("/integralManagelog")
public class MememberIntegralLogController {
	
	
	@Autowired
	private MememberIntegralLogService mememberIntegralLogService;
    @Autowired
    RedisClientTemplate redisClientTemplate;
	
	 /**
    *
    * @param form
    * @return
    */
   @ApiOperation(value = "获取积分流水信息列表")
   @ResponseBody
   @RequestMapping(value = "/queryMemberBaseList")
   public RestResult<PageResult<MememberIntegralLogResponse>> queryMemberBaseList(@RequestBody MememberIntegralLogQueryRequest form) {
       String token = form.getToken();
       String memStr = redisClientTemplate.get(token);
       if (StringUtils.isEmpty(memStr)) {
           return RestResult.error("4000", "登录失效");
       }
       MememberUserRequest mMember = JSONArray.parseObject(memStr, MememberUserRequest.class);
       Page page = new Page();
       page.setLimit(form.getLimit());
       page.setStart(form.getStart());
       MememberIntegralLog mememberIntegralLog = BeanUtil.copy(form, MememberIntegralLog.class);
       mememberIntegralLog.setMemberUserId(mMember.getId());
       PageResult<MememberIntegralLog> result = mememberIntegralLogService.queryPage(mememberIntegralLog, page);
       List<MememberIntegralLogResponse> list = result.getRows().stream().map(temp -> {
    	   MememberIntegralLogResponse mememberIntegralLogResponse = BeanUtil.copy(temp, MememberIntegralLogResponse.class);
           return mememberIntegralLogResponse;
       }).filter(item -> item != null).collect(Collectors.toList());
       PageResult<MememberIntegralLogResponse> pageResult = new PageResult<>();
       pageResult.setRows(list);
       pageResult.setTotal(result.getTotal()); 
       return RestResult.success(pageResult);
   }

}
